package com.example.lib_audio.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.viewbinding.ViewBinding
import com.example.lib_audio.R
import com.example.lib_audio.bottom.AlbumRotateAnimation
import com.example.lib_audio.bottom.ImageLoader
import com.example.lib_audio.databinding.ActivityMusicPlayBinding
import com.example.lib_audio.events.AudioFavoriteStatusChangeEvent
import com.example.lib_audio.events.AudioLoadEvent
import com.example.lib_audio.events.AudioPauseEvent
import com.example.lib_audio.events.AudioPlayModeChangeEvent
import com.example.lib_audio.events.AudioProgressChangeEvent
import com.example.lib_audio.events.AudioStartEvent
import com.example.lib_audio.player.AudioController
import com.example.lib_data.base.activity.BaseActivity
import com.example.lib_data.bmob.Song
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MusicPlayActivity : BaseActivity() {
    private lateinit var mRotateAnimation:AlbumRotateAnimation
    private val binding:ActivityMusicPlayBinding by lazy {
        mBinding as ActivityMusicPlayBinding
    }
    override fun getLayoutBinding(): ViewBinding? {
        return ActivityMusicPlayBinding.inflate(layoutInflater)
    }

    override fun initUI() {
        //注册EventBus
        EventBus.getDefault().register(this)
        //创建旋转动画对象
        mRotateAnimation = AlbumRotateAnimation(binding.albumView)
        //关闭当前界面
        binding.closeView.setOnClickListener { finish() }
        //下一曲
        binding.nextView.setOnClickListener { AudioController.instance.playNext() }
        //上一曲
        binding.previousView.setOnClickListener { AudioController.instance.playPrevious() }
        //播放或者暂停
        binding.playView.setOnClickListener { AudioController.instance.playOrPause() }

        //初始化当前音乐信息
        initLoadUI(AudioController.instance.getNowPlayingMusic())
        if (AudioController.instance.isPlaying()){
            showPlayStatus()
        }

        //切换播放模式
        binding.modeView.setOnClickListener {
            AudioController.instance.changePlayMode()
        }

        //收藏和取消收藏
        binding.favoriteView.setOnClickListener {
            AudioController.instance.changeFavoriteStatus()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLoadEvent(event: AudioLoadEvent){
        initLoadUI(event.music)
        mRotateAnimation.reset()
        binding.progressView.reset()
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onStartEvent(event: AudioStartEvent){
        showPlayStatus()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPauseEvent(event: AudioPauseEvent){
        showPauseStatus()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onProgressChangeEvent(event: AudioProgressChangeEvent){
        binding.timeStartView.text = TimeUtils.stringFromTime(event.current)
        binding.timeEndView.text = TimeUtils.stringFromTime(event.total)
        binding.progressView.changeProgress(event.current.toFloat()/event.total.toFloat())
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPlayModeChangeEvent(event: AudioPlayModeChangeEvent){
        when(event.mode){
            AudioController.PlayMode.RANDOM ->{binding.modeView.setImageResource(R.drawable.audio_mode_random)}
            AudioController.PlayMode.LOOP ->{binding.modeView.setImageResource(R.drawable.audio_mode_list_cycle)}
            AudioController.PlayMode.REPEAT ->{binding.modeView.setImageResource(R.drawable.audio_mode_recycle)}
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onFavoriteStatusChangeEvent(event: AudioFavoriteStatusChangeEvent){
        if (event.isFavorite){
            binding.favoriteView.setImageResource(R.drawable.audio_selected_love)
        }else{
            binding.favoriteView.setImageResource(R.drawable.audio_love)
        }
    }
    /**
     * 播放状态的操作
     */
    private fun showPlayStatus(){
        mRotateAnimation.start()
        binding.playView.setImageResource(R.drawable.audio_stop)
        IndicatorAnimation.startAnimation(binding.indicatorView)
    }

    /**
     * 暂停状态的操作
     */
    private fun showPauseStatus(){
        mRotateAnimation.pause()
        IndicatorAnimation.stopAnimation(binding.indicatorView)
        binding.playView.setImageResource(R.drawable.audio_play)
    }

    /**
     * 初始化音乐界面
     */
    private fun initLoadUI(music: Song){
        binding.titleView.text = music.title
        binding.authorView.text = music.author
        binding.timeEndView.text = TimeUtils.stringFromTime(music.duration)
        ImageLoader.loadCircleImage(music.album,binding.albumView)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}